import {request} from "@umijs/max";
import {PageParam} from "@/services/common";
import {covertTableData} from "@/utils/tableUtil";

export interface GoodsVO {
        /**
        * id
        */
        id: string;
        /**
        * 商品类型
        */
        goodsType: string;
        /**
        * 商品名称
        */
        goodsName?: string;
        /**
        * 商品描述
        */
        goodsDesc?: string;
        /**
        * 价格
        */
        price: number;
        /**
        * 创建时间
        */
        createTime?: string;
        /**
        * 更新时间
        */
        updateTime?: string;
}

export interface GoodsQueryDTO extends PageParam {
        /**
        * id
        */
        id?: string;
        /**
        * 商品名称
        */
        goodsName?: string;
}
export interface GoodsUpdateDTO {
        /**
        * id
        */
        id?: string;
        /**
        * 商品类型
        */
        goodsType: string;
        /**
        * 商品名称
        */
        goodsName?: string;
        /**
        * 商品描述
        */
        goodsDesc?: string;
        /**
        * 价格
        */
        price: number;
        /**
        * 创建时间
        */
        createTime?: string;
        /**
        * 更新时间
        */
        updateTime?: string;
}

export const getGoodsById = async (id: string) => {
    return request<GoodsVO>(`/api/goods?id=${id}`, {method: 'GET'})
}

export const getGoodsList = async (params?: GoodsQueryDTO) => {
    return request<Array<GoodsVO>>(`/api/goods/list`, {method: 'GET', params})
}

export const getGoodsPage = async (params: GoodsQueryDTO) => {
    let response = await request<Array<GoodsVO>>(`/api/goods/page`, {method: 'GET', params})
    return covertTableData(response)
}

export const updateGoods = async (goodsUpdateDTO: GoodsUpdateDTO) => {
    return request<GoodsVO>(`/api/goods/update`, {method: 'POST', data: goodsUpdateDTO})
}

export const saveGoods = async (goodsUpdateDTO: GoodsUpdateDTO) => {
    return request<GoodsVO>(`/api/goods/save`, {method: 'POST', data: goodsUpdateDTO})
}

export const removeGoods = async (id: string) => {
    return request<boolean>(`/api/goods/remove?id=${id}`, {method: 'POST'})
}
